<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mcron extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "cron";
    public $primaryKey = "cron_id";
    public $resCode = "CRON";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
		$fieldData['cron_name'] = '';
		$fieldData['cron_class'] = '';
		$fieldData['cron_method'] = '';
		$fieldData['cron_status'] = 0;
		$fieldData['cron_rules'] = '';
		$fieldData['cron_next_run'] = 0;
		return $fieldData;
    }
	
	function calc_time($days =array(), $weeks = array(), $hours =array(), $minutes = array()){
		$mtime = time();
		$mday = date("d",$mtime);
		$mmonth = date("m",$mtime);
		$myear = date("Y",$mtime);
		$result = array();
		$sday = $mday;
		$smonth = $mmonth;
		$syear = $myear;
		if(in_array(-1,$days)){
			$days = array($mday);
			
		}
		if($days)
		foreach($days as $d){
			$smonth = $mmonth;
			//ngày của tháng sau
			if($d <$mday){
				$smonth = $mmonth +1;
			}
			if($hours){
				foreach($hours as $hour){
					if($minutes){
						foreach($minutes as $minute){
							//hours, minute, second
							$x = mktime($hour,$minute,0,$smonth,$d,$myear);
							$result[] = $x;
						}
					}
				}
			}
		}
		if($weeks)
		foreach($weeks as $w){
			$current = strtotime($w);
			$smonth = date("m",$current);
			$d = date("d",$current);
			$myear = date("Y",$current);
			if($hours){
				foreach($hours as $hour){
					if($minutes){
						foreach($minutes as $minute){
							//hours, minute, second
							$x = mktime($hour,$minute,0,$smonth,$d,$myear);
							$result[] = $x;
						}
					}
				}
			}
		}
		sort($result);
		return $result;
	}
	
}

?>